package com.assignwork.service.impl;

import com.assignwork.dto.DailyPlanRemarkSubmitDTO;
import com.assignwork.entity.DailyPlanRemark;
import com.assignwork.mapper.DailyPlanRemarkMapper;
import com.assignwork.service.IDailyPlanRemarkService;
import com.assignwork.vo.DailyPlanRemarkVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

/**
 * @ClassName DailyPlanRemarkServiceImpl
 * @Description 昼夜计划备注接口实现
 * @Author dujianyu
 * @Date 2023/12/24 14:40
 */
@Service
public class DailyPlanRemarkServiceImpl extends BaseServiceImpl<DailyPlanRemarkMapper, DailyPlanRemark> implements IDailyPlanRemarkService {
    @Override
    public boolean submitRemark(DailyPlanRemarkSubmitDTO dto) {
        QueryWrapper<DailyPlanRemark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(DailyPlanRemark::getPlanId, dto.getPlanId())
                .eq(DailyPlanRemark::getIsDeleted, 0)
                .last("limit 1");
        DailyPlanRemark exist = baseMapper.selectOne(queryWrapper);
        if (!Func.isNull(exist) && !Func.isNull(exist.getId())) {
            exist.setRemark(dto.getRemark());
            return super.saveOrUpdate(exist);
        } else {
            DailyPlanRemark entity = BeanUtil.copy(dto, DailyPlanRemark.class);
            return super.saveOrUpdate(entity);
        }

    }

    @Override
    public DailyPlanRemarkVO remarkByPlanId(Long planId) {
        QueryWrapper<DailyPlanRemark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(DailyPlanRemark::getId, DailyPlanRemark::getPlanId, DailyPlanRemark::getRemark)
                .eq(DailyPlanRemark::getIsDeleted,0)
                .eq(DailyPlanRemark::getPlanId,planId)
                .orderByDesc(DailyPlanRemark::getCreateTime)
                .last("limit 1");
        DailyPlanRemark entity = baseMapper.selectOne(queryWrapper);

        DailyPlanRemarkVO res = new DailyPlanRemarkVO();
        if (Func.isNull(entity)) {
            res.setRemark("");
        } else {
            res.setId(entity.getId());
            res.setPlanId(entity.getPlanId());
            res.setRemark(entity.getRemark());
        }
        return res;
    }
}
